//
//  SmallDecisionsSzView.h
//  SmallDecisions
//
//  Created by 李付雄 on 2025/8/13.
//

#import "SmallDecisionsSzView.h"

#define SLW 40
#define Margin 2.5

@interface SmallDecisionsSzView()

@property (nonatomic, strong)UIImageView *hqImageView;
@property (nonatomic, strong)NSArray *diceValues;

@end

@implementation SmallDecisionsSzView

- (instancetype)initWithDiceCount:(NSInteger)count {
    self = [super init];
    if (self) {
        self.diceCount = count;
        [self startDisplay];
    }
    return self;
}

- (NSArray *)generateRandomDiceValues {
    NSMutableArray *values = [NSMutableArray array];
    for (int i = 0; i < self.diceCount; i++) {
        [values addObject:@(arc4random_uniform(6) + 1)];
    }
    return [values copy];
}

- (void)startDisplay {
    self.diceValues = [self generateRandomDiceValues];
    switch (self.diceCount) {
        case 1:
            [self displayOneDice];
            break;
        case 2:
            [self displayTwoDice];
            break;
        case 3:
            [self displayThreeDice];
            break;
        case 4:
            [self displayFourDice];
            break;
        case 5:
            [self displayFiveDice];
            break;
        case 6:
            [self displaySixDice];
            break;
        default:
            NSLog(@"Invalid dice count. Please enter a number between 1 and 6.");
            break;
    }
}

- (UIImageView *)getDiceImageViewWithValue:(NSString *)value {
    UIImageView *diceImageView = [[UIImageView alloc] init];
    NSString *slImageName = [NSString stringWithFormat:@"sz_dice_img_%@",value];
    diceImageView.image = [UIImage imageNamed:slImageName];
    diceImageView.contentMode = UIViewContentModeScaleAspectFit;
    [self addSubview:diceImageView];
    return diceImageView;
}

- (void)displayOneDice {
    UIImageView *diceImageView = [self getDiceImageViewWithValue:self.diceValues[0]];
    [diceImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_offset(0);
        make.height.width.mas_offset(SLW);
    }];
}

- (void)displayTwoDice {
    UIImageView *diceImageView1 = [self getDiceImageViewWithValue:self.diceValues[0]];
    [diceImageView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_offset(0);
        make.height.width.mas_offset(SLW);
        make.right.equalTo(self.mas_centerX).offset(-Margin);
    }];

    UIImageView *diceImageView2 = [self getDiceImageViewWithValue:self.diceValues[1]];
    [diceImageView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.height.width.equalTo(diceImageView1);
        make.left.equalTo(self.mas_centerX).offset(Margin);
    }];
}

- (void)displayThreeDice {
    UIImageView *diceImageView1 = [self getDiceImageViewWithValue:self.diceValues[0]];
    [diceImageView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mas_centerY).offset(-Margin);
        make.height.width.mas_offset(SLW);
        make.centerX.mas_offset(0);
    }];

    UIImageView *diceImageView2 = [self getDiceImageViewWithValue:self.diceValues[1]];
    [diceImageView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(diceImageView1.mas_bottom).offset(Margin);
        make.height.width.equalTo(diceImageView1);
        make.right.equalTo(diceImageView1.mas_centerX).offset(-Margin);
    }];
    UIImageView *diceImageView3 = [self getDiceImageViewWithValue:self.diceValues[2]];
    [diceImageView3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.height.width.equalTo(diceImageView2);
        make.left.equalTo(diceImageView1.mas_centerX).offset(Margin);
    }];
}

- (void)displayFourDice {
    UIImageView *diceImageView1 = [self getDiceImageViewWithValue:self.diceValues[0]];
    [diceImageView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mas_centerY).offset(-Margin);
        make.height.width.mas_offset(SLW);
        make.right.equalTo(self.mas_centerX).offset(-Margin);
    }];

    UIImageView *diceImageView2 = [self getDiceImageViewWithValue:self.diceValues[1]];
    [diceImageView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.height.width.equalTo(diceImageView1);
        make.left.equalTo(diceImageView1.mas_right).offset(Margin);
    }];

    UIImageView *diceImageView3 = [self getDiceImageViewWithValue:self.diceValues[2]];
    [diceImageView3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.height.width.equalTo(diceImageView1);
        make.top.equalTo(diceImageView1.mas_bottom).offset(Margin);
    }];

    UIImageView *diceImageView4 = [self getDiceImageViewWithValue:self.diceValues[3]];
    [diceImageView4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.height.width.equalTo(diceImageView2);
        make.top.equalTo(diceImageView3);
    }];
}

- (void)displayFiveDice {
    UIImageView *diceImageView1 = [self getDiceImageViewWithValue:self.diceValues[0]];
    [diceImageView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_offset(0);
        make.height.width.mas_offset(SLW);
    }];

    UIImageView *diceImageView2 = [self getDiceImageViewWithValue:self.diceValues[1]];
    [diceImageView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(diceImageView1.mas_left);
        make.height.width.equalTo(diceImageView1);
        make.bottom.equalTo(diceImageView1.mas_top).offset(Margin * 2);
    }];

    UIImageView *diceImageView3 = [self getDiceImageViewWithValue:self.diceValues[2]];
    [diceImageView3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.height.width.equalTo(diceImageView2);
        make.left.equalTo(diceImageView1.mas_right);
    }];

    UIImageView *diceImageView4 = [self getDiceImageViewWithValue:self.diceValues[3]];
    [diceImageView4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.height.width.equalTo(diceImageView2);
        make.top.equalTo(diceImageView1.mas_bottom).offset(-Margin* 2);
    }];

    UIImageView *diceImageView5 = [self getDiceImageViewWithValue:self.diceValues[4]];
    [diceImageView5 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.height.width.equalTo(diceImageView3);
        make.top.equalTo(diceImageView4);
    }];
}

- (void)displaySixDice {
    UIImageView *diceImageView1 = [self getDiceImageViewWithValue:self.diceValues[0]];
    [diceImageView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mas_centerY);
        make.height.width.mas_offset(SLW);
        make.centerX.mas_offset(0);
    }];

    UIImageView *diceImageView2 = [self getDiceImageViewWithValue:self.diceValues[1]];
    [diceImageView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(diceImageView1.mas_left).offset(-Margin * 2);
        make.top.height.width.equalTo(diceImageView1);
    }];

    UIImageView *diceImageView3 = [self getDiceImageViewWithValue:self.diceValues[2]];
    [diceImageView3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.height.width.equalTo(diceImageView2);
        make.left.equalTo(diceImageView1.mas_right).offset(Margin * 2);
    }];

    UIImageView *diceImageView4 = [self getDiceImageViewWithValue:self.diceValues[3]];
    [diceImageView4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.height.width.equalTo(diceImageView1);
        make.top.equalTo(diceImageView1.mas_bottom).offset(Margin);
    }];

    UIImageView *diceImageView5 = [self getDiceImageViewWithValue:self.diceValues[4]];
    [diceImageView5 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.height.width.equalTo(diceImageView2);
        make.top.equalTo(diceImageView4);
    }];

    UIImageView *diceImageView6 = [self getDiceImageViewWithValue:self.diceValues[5]];
    [diceImageView6 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.height.width.equalTo(diceImageView3);
        make.top.equalTo(diceImageView4);
    }];
}


@end
